Page({
    data: {
      imageSrc: [],
      timestamp: null,
      location: null
    },
  
    takePhoto: function() {
      const that = this;
      
      wx.chooseImage({
        count: 9,
        sizeType: ['original', 'compressed'],
        sourceType: ['camera', 'album'],
        success: function(res) {
          const tempFilePaths = res.tempFilePaths;
          const currentTime = new Date().toLocaleString();
          
          // 获取位置信息
          wx.getLocation({
            type: 'wgs84',
            success: (locRes) => {
              that.setData({
                imageSrc: tempFilePaths,
                timestamp: currentTime,
                location: `${locRes.latitude.toFixed(4)}, ${locRes.longitude.toFixed(4)}`
              });
            },
            fail: () => {
              that.setData({
                imageSrc: tempFilePaths,
                timestamp: currentTime,
                location: '位置获取失败'
              });
            }
          });
        },
        fail: (err) => {
          wx.showToast({
            title: '选择失败，请重试',
            icon: 'none'
          });
          console.error('媒体选择错误：', err);
        }
      });
    },
  
    uploadPhoto: function() {
      if (this.data.imageSrc.length === 0) {
        wx.showToast({
          title: '请先选择照片',
          icon: 'none'
        });
        return;
      }
  
      // 使用缓存存储图片路径
      try {
        wx.setStorageSync('cachedImages', this.data.imageSrc);
        wx.navigateTo({
          url: `/pages/result/result?timestamp=${this.data.timestamp}&location=${encodeURIComponent(this.data.location)}`
        });
      } catch (e) {
        wx.showToast({
          title: '数据存储失败',
          icon: 'none'
        });
      }
    },
  
    clearPhoto: function() {
      this.setData({
        imageSrc: [],
        timestamp: null,
        location: null
      });
      wx.showToast({
        title: '已清除',
        icon: 'success',
        duration: 1500
      });
    }
  });

  